#include	"app.h"
#include	"kernel.h"
#include	"scidriver.h"
#include	"sci.h"
#include 	"module.h"
#include	"macro.h"

INT8U ubUserDataBuf[cSciNumMax][cUserBufSize];				
INT8U ubCommandBuffer[cSciNumMax][MAX_COMMAND_LENGTH];
INT8U *pbCommandIn[cSciNumMax];
INT8U ubCommandLength[cSciNumMax];
INT8U ubTransferData[cSciNumMax][MAX_COMMAND_LENGTH];		// 传输保存帧数据
INT8U ubTransferLength[cSciNumMax];							// 传输保存帧数据长度


INT8U ubSciRcvSts[cSciNumMax];							  	// 当前帧处理状态
INT8U uwFrameBrkCnt[cSciNumMax];							// 帧中断计时器
INT8U uwFrameLenth[cSciNumMax];								// 帧数据长度
INT8U ubFrameVerion[cSciNumMax];							// 帧版本

union BTN_MATRIX1 BtnMatrix1[cBtnMax];
union BTN_MATRIX2 BtnMatrix2[cBtnMax];
union BTN_MATRIX3 BtnMatrix3[cBtnMax];
union BTN_MATRIX1 BtnMatrix1Old[cBtnMax];
union BTN_MATRIX2 BtnMatrix2Old[cBtnMax];
union BTN_MATRIX3 BtnMatrix3Old[cBtnMax];

INT16U uwTest;
INT16U uwTest1;
INT16U uwTest2;

static TASK_EVENT Sci_Event = 0;

uint8_t   subNumAsciiPick(uint32_t udwNumber, uint8_t ubPosition);
uint8_t subNumToXX(const uint16_t uwNumber, const int8_t bExponent, uint8_t *pubBuffer);
uint8_t subNumToXXXXX(uint32_t udwNumber, int8_t bExponent, uint8_t *pubBuffer);
uint32_t  sudwNumFormat(uint32_t udwNumber, int8_t bExponent, int8_t bDecimal);
uint8_t Int16ToDecAscii_Optimized(int16_t value, char* buffer);
uint8_t SafeInt16ToDecAscii(int16_t value, char* buffer);


void sSciInitial(void);
void sSciDebug(void);


void sParsing(INT8U sciid);
INT8U subFindComm(INT8U sciid);
void  sSciChk(uint8_t sciid);

void sSciNAKCommand(INT8U sciid);
void sSciTCommand(INT8U sciid);
void sTCGQ1Command(INT8U sciid);
void sTCGQ2Command(INT8U sciid);
void sTSAVECommand(INT8U sciid);
void sTSEATCommand(INT8U sciid);
void sTSTBKCommand(INT8U sciid);
void sTCUPCommand(INT8U sciid);
void sTWATERCommand(INT8U sciid);
void sTP1Command(INT8U sciid);
void sTLPCommand(INT8U sciid);




const struct COMARRAY
{
	INT8U com[cGenComLength];
	void (*SUB)(INT8U sciid);
}ComTab[] = {
				 "     ",    sSciNAKCommand,
	             "A    ",    sSciNAKCommand,
	             "T    ",    sSciTCommand,                 
		      };

#define cCommandMax   (sizeof(ComTab)/sizeof(struct COMARRAY))



void sSciTask(void)
{
	// sci初始化
	sSciInitial();
    while(1)
    {
        Sci_Event = OSEventGet();
        if(OSEventConfirm(Sci_Event,eSciTimer))
        {
//			sMainBrdSciRec(ComDevice2);		// 接收程控器数据

		
			sRcvSciProcess(ComDevice1);		// 接收主按键板信息
			sRcvSciProcess(ComMainBrd);		// 接收副按键板信息
			sLogicProcess();				// 根据状态逻辑判断
			sFuncBtnLogicPorcess();			// 逻辑状态决定按键板的LED开关
			sBtnLedSend(ComDevice1);		// led状态发生改变，则发送数据到按键板
			sBtnLedSend(ComMainBrd);
			sSciDebug();
		}

	}
}


void sSciInitial(void)
{
	sInitialSci(ComMainBrd , MAX_COMMAND_LENGTH);
	sInitialSci(ComDevice1 , MAX_COMMAND_LENGTH);
	sInitialSci(ComDevice2 , MAX_COMMAND_LENGTH);
	sInitialSci(ComDevice3 , MAX_COMMAND_LENGTH);

	pbCommandIn[ComMainBrd] = ubCommandBuffer[ComMainBrd];
	pbCommandIn[ComDevice1] = ubCommandBuffer[ComDevice1];
	pbCommandIn[ComDevice2] = ubCommandBuffer[ComDevice2];
	pbCommandIn[ComDevice3] = ubCommandBuffer[ComDevice3];

	ubCommandLength[ComMainBrd] = 0;
	ubCommandLength[ComDevice1] = 0;
	ubCommandLength[ComDevice2] = 0;
	ubCommandLength[ComDevice3] = 0;

	ubSciRcvSts[ComMainBrd] = cFramehead;
	ubSciRcvSts[ComDevice1] = cFramehead;
	ubSciRcvSts[ComDevice2] = cFramehead;
	ubSciRcvSts[ComDevice3] = cFramehead;
	
	SysBit_1.Bits.SciIniOK = 1; // 防止sci未初始化，中断接收出现野指针
}


void sSciNAKCommand(INT8U sciid)		//NAK
{
    ubUserDataBuf[sciid][0]  = 'N';
	ubUserDataBuf[sciid][1]  = 'A';
	ubUserDataBuf[sciid][2]  = 'K';

	ubUserDataBuf[sciid][3]  = 0x0D;
	sSciWrite(sciid,ubUserDataBuf[sciid],4);
}


void sSciTCommand(INT8U sciid)		//
{
	uint8_t ubCmdTemp1;
    uint8_t ubCmdTemp2;
    uint8_t ubCmdTemp3;
    uint8_t ubCmdTemp4;
	uint8_t ubCmdTemp5;
//	uint8_t ubCmdTemp6;

    ubCmdTemp1 = ubCommandBuffer[sciid][1];
    ubCmdTemp2 = ubCommandBuffer[sciid][2];
    ubCmdTemp3 = ubCommandBuffer[sciid][3];
    ubCmdTemp4 = ubCommandBuffer[sciid][4];
	ubCmdTemp5 = ubCommandBuffer[sciid][5];
//	ubCmdTemp6 = ubCommandBuffer[sciid][6];

	if(ubCmdTemp1 == 'C' && ubCmdTemp2 == 'G' && ubCmdTemp3 == 'Q' && ubCmdTemp4 == '1')
    {
        sTCGQ1Command(sciid);
    }
	else if(ubCmdTemp1 == 'C' && ubCmdTemp2 == 'G' && ubCmdTemp3 == 'Q' && ubCmdTemp4 == '2')
    {
        sTCGQ2Command(sciid);
    }
	else if(ubCmdTemp1 == 'S' && ubCmdTemp2 == 'A' && ubCmdTemp3 == 'V' && ubCmdTemp4 == 'E')
    {
        sTSAVECommand(sciid);
    }
	else if(ubCmdTemp1 == 'S' && ubCmdTemp2 == 'E' && ubCmdTemp3 == 'A' && ubCmdTemp4 == 'T')
    {
        sTSEATCommand(sciid);
    }
	else if(ubCmdTemp1 == 'S' && ubCmdTemp2 == 'T' && ubCmdTemp3 == 'B' && ubCmdTemp4 == 'K')
    {
        sTSTBKCommand(sciid);
    }
	else if(ubCmdTemp1 == 'C' && ubCmdTemp2 == 'U' && ubCmdTemp3 == 'P')
    {
        sTCUPCommand(sciid);
    }
	else if(ubCmdTemp1 == 'W' && ubCmdTemp2 == 'A' && ubCmdTemp3 == 'T' && ubCmdTemp4 == 'E' && ubCmdTemp5 == 'R')
    {
        sTWATERCommand(sciid);
    }
	else if(ubCmdTemp1 == 'P' && ubCmdTemp2 == '1')
    {
        sTP1Command(sciid);
    }
	else if(ubCmdTemp1 == 'L' && ubCmdTemp2 == 'P')
    {
       sTLPCommand(sciid);
    }
	else
    {
//		sSciNAKCommand(sciid);
    }
}



void sSciDebug(void)
{
	if(sSciGetTxStatus(ComDevice3) == cSciTxRdy)
	{
		sSciChk(ComDevice3);
	}

}


/*******************************************************************************
Function name  : sSciChk()
Description    : the function below is used to receive commands
Parameters     : sciid
Return         : none
*******************************************************************************/
void sSciChk(uint8_t sciid)
{
	INT8U ubSciTemp;
	
	while(1)
	{
		ubSciTemp = sSciRead(sciid,pbCommandIn[sciid]);  //receive data
			
		if(ubSciTemp == cSciRxBufEmpty)
		{
			return;
		}
		
		if(ubCommandLength[sciid] >= MAX_COMMAND_LENGTH)
		{
			pbCommandIn[sciid] = ubCommandBuffer[sciid];   
			ubCommandLength[sciid] = 0;
		}
		else if(((*pbCommandIn[sciid]) == 0x0d) || ((*pbCommandIn[sciid]) == 0x0A))  //ascii CR(0D),LF(0A)
		{                                             
			*pbCommandIn[sciid] = 0x0d; 
   
			sParsing(sciid);  //explain the receive cmd			
			pbCommandIn[sciid] = ubCommandBuffer[sciid];   
			ubCommandLength[sciid] = 0;
		    return;
		}
		else
		{
			ubCommandLength[sciid] ++;
			pbCommandIn[sciid] ++;
		}
	}
}

void sParsing(INT8U sciid)
{
	ComTab[subFindComm(sciid)].SUB(sciid);				
}	



/*********************************************************************
*looking for the command
*********************************************************************/
INT8U subFindComm(INT8U sciid)
{
    INT8U i = 0;
    INT8U j = 0;
    
    while (ComTab[j++].com[0] != '*')
	{
		for(i = 0; i<(cGenComLength - 1); i++)
		{	
			if(ComTab[j].com[i] == ubCommandBuffer[sciid][i])	
			{
                if(ComTab[j].com[i + 1] == ' ')
                {
					return(j);
				}
			}
			else
			{
              break;
			}
		}
		
		if(j > cCommandMax) return(0); // no the command
	}
	//after ** command
	return(0);				
}


void sTSAVECommand(INT8U sciid) // 保存数据
{
	INT8U  ubStrLen;
	INT8U  *pDataBuf;

	ubStrLen = 0;

	// 1
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.P1.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	// 2
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.P1.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	// 3
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.P2.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	// 4
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.P2.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	// 5
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.P3.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	// 6
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.P3.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	// 7
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.UpLmt.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	// 8
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.UpLmt.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	// 9
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.DnLmt.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	// 10
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.DnLmt.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	

	sSciWrite(sciid,ubUserDataBuf[sciid],ubStrLen);

}


void sTSEATCommand(INT8U sciid) // 座椅相关数据
{
	INT8U  ubStrLen;
	INT8U  *pDataBuf;

	ubStrLen = 0;

	ubUserDataBuf[sciid][ubStrLen++] = 'S';
	ubUserDataBuf[sciid][ubStrLen++] = 'E';
	ubUserDataBuf[sciid][ubStrLen++] = 'A';
	ubUserDataBuf[sciid][ubStrLen++] = 'T';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'M';		// 模式
	ubUserDataBuf[sciid][ubStrLen++] = 'O';
	ubUserDataBuf[sciid][ubStrLen++] = 'D';
	ubUserDataBuf[sciid][ubStrLen++] = 'E';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	ubUserDataBuf[sciid][ubStrLen++] = uwMotorCtrMode + '0';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';	

	ubUserDataBuf[sciid][ubStrLen++] = 'H';		// 座椅限位硬件检测
	ubUserDataBuf[sciid][ubStrLen++] = 'L';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	if(LclBtnSts1.Bits.LmtPstSeatUp) 
	{
		ubUserDataBuf[sciid][ubStrLen++] = '1';
	}
	else
	{
		ubUserDataBuf[sciid][ubStrLen++] = '0';
	}
	
	if(LclBtnSts1.Bits.LmtPstSeatDown) 
	{
		ubUserDataBuf[sciid][ubStrLen++] = '1';
	}
	else
	{
		ubUserDataBuf[sciid][ubStrLen++] = '0';
	}
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'U';		// 设定限制
	ubUserDataBuf[sciid][ubStrLen++] = 'L';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.UpLmt.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	ubUserDataBuf[sciid][ubStrLen++] = 'D';
	ubUserDataBuf[sciid][ubStrLen++] = 'L';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.DnLmt.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'C';		//当前位置
	ubUserDataBuf[sciid][ubStrLen++] = 'u';
	ubUserDataBuf[sciid][ubStrLen++] = 'r';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)CurrPst.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'B';		//按键
	ubUserDataBuf[sciid][ubStrLen++] = 'T';
	ubUserDataBuf[sciid][ubStrLen++] = 'N'; 
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	if(SysSts1.Bits.Up)
	{	
		ubUserDataBuf[sciid][ubStrLen++] = 'U';
		ubUserDataBuf[sciid][ubStrLen++] = 'P';
	}
	else if(SysSts1.Bits.Down)
	{	
		ubUserDataBuf[sciid][ubStrLen++] = 'D';
		ubUserDataBuf[sciid][ubStrLen++] = 'N';
	}
	else
	{	
		ubUserDataBuf[sciid][ubStrLen++] = ' ';
		ubUserDataBuf[sciid][ubStrLen++] = ' ';
	}
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'S';		//运行状态1
	ubUserDataBuf[sciid][ubStrLen++] = 'T';
	ubUserDataBuf[sciid][ubStrLen++] = 'S'; 
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	ubUserDataBuf[sciid][ubStrLen++] = uwSeatRunSts + '0';
	ubUserDataBuf[sciid][ubStrLen++] = uwSeatRunActSts + '0';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	sSciWrite(sciid,ubUserDataBuf[sciid],ubStrLen);

}

void sTSTBKCommand(INT8U sciid) // 座椅相关数据
{
	INT8U  ubStrLen;
	INT8U  *pDataBuf;

	ubStrLen = 0;

	ubUserDataBuf[sciid][ubStrLen++] = 'S';
	ubUserDataBuf[sciid][ubStrLen++] = 'T';
	ubUserDataBuf[sciid][ubStrLen++] = 'B';
	ubUserDataBuf[sciid][ubStrLen++] = 'K';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'M';		// 模式
	ubUserDataBuf[sciid][ubStrLen++] = 'O';
	ubUserDataBuf[sciid][ubStrLen++] = 'D';
	ubUserDataBuf[sciid][ubStrLen++] = 'E';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	ubUserDataBuf[sciid][ubStrLen++] = uwMotorCtrMode + '0';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';	

	ubUserDataBuf[sciid][ubStrLen++] = 'H';		// 座椅限位硬件检测
	ubUserDataBuf[sciid][ubStrLen++] = 'L';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	if(LclBtnSts1.Bits.LmtPstStBkUp) 
	{
		ubUserDataBuf[sciid][ubStrLen++] = '1';
	}
	else
	{
		ubUserDataBuf[sciid][ubStrLen++] = '0';
	}
	
	if(LclBtnSts1.Bits.LmtPstStBkDown) 
	{
		ubUserDataBuf[sciid][ubStrLen++] = '1';
	}
	else
	{
		ubUserDataBuf[sciid][ubStrLen++] = '0';
	}
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'U';		// 设定限制
	ubUserDataBuf[sciid][ubStrLen++] = 'L';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.UpLmt.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	ubUserDataBuf[sciid][ubStrLen++] = 'D';
	ubUserDataBuf[sciid][ubStrLen++] = 'L';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.DnLmt.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'C';		//当前位置
	ubUserDataBuf[sciid][ubStrLen++] = 'u';
	ubUserDataBuf[sciid][ubStrLen++] = 'r';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)CurrPst.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'B';		//按键
	ubUserDataBuf[sciid][ubStrLen++] = 'T';
	ubUserDataBuf[sciid][ubStrLen++] = 'N'; 
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	if(SysSts1.Bits.Bu)
	{	
		ubUserDataBuf[sciid][ubStrLen++] = 'U';
		ubUserDataBuf[sciid][ubStrLen++] = 'P';
	}
	else if(SysSts1.Bits.Bn)
	{	
		ubUserDataBuf[sciid][ubStrLen++] = 'D';
		ubUserDataBuf[sciid][ubStrLen++] = 'N';
	}
	else
	{	
		ubUserDataBuf[sciid][ubStrLen++] = ' ';
		ubUserDataBuf[sciid][ubStrLen++] = ' ';
	}
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'S';		//运行状态1
	ubUserDataBuf[sciid][ubStrLen++] = 'T';
	ubUserDataBuf[sciid][ubStrLen++] = 'S'; 
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	ubUserDataBuf[sciid][ubStrLen++] = uwStBkRunSts + '0';
	ubUserDataBuf[sciid][ubStrLen++] = uwStBkRunActSts + '0';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	sSciWrite(sciid,ubUserDataBuf[sciid],ubStrLen);

}


void sTCUPCommand(INT8U sciid) // CUP设置
{
	INT8U  ubStrLen;
	INT8U  *pDataBuf;

	ubStrLen = 0;

	ubUserDataBuf[sciid][ubStrLen++] = 'C';
	ubUserDataBuf[sciid][ubStrLen++] = 'U';
	ubUserDataBuf[sciid][ubStrLen++] = 'P';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'S';		// 模式
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = 's';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	ubUserDataBuf[sciid][ubStrLen++] = uwSetStatus + '0';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';	

	ubUserDataBuf[sciid][ubStrLen++] = 'C';		// CUP状态
	ubUserDataBuf[sciid][ubStrLen++] = 'u';
	ubUserDataBuf[sciid][ubStrLen++] = 'p';
	ubUserDataBuf[sciid][ubStrLen++] = ':';		// 
	if(SysSts1.Bits.Cup)
	{
		ubUserDataBuf[sciid][ubStrLen++] = 'O';
		ubUserDataBuf[sciid][ubStrLen++] = 'N';
	}
	else
	{
		ubUserDataBuf[sciid][ubStrLen++] = 'O';
		ubUserDataBuf[sciid][ubStrLen++] = 'F';
		ubUserDataBuf[sciid][ubStrLen++] = 'F';
	}
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	ubUserDataBuf[sciid][ubStrLen++] = 'S';		// 设置的时间
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.CupTime, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'T';		// CUP时间
	ubUserDataBuf[sciid][ubStrLen++] = 'i';
	ubUserDataBuf[sciid][ubStrLen++] = 'm';
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)udwCupCnt, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	sSciWrite(sciid,ubUserDataBuf[sciid],ubStrLen);

}


void sTWATERCommand(INT8U sciid) // CUP设置
{
	INT8U  ubStrLen;
	INT8U  *pDataBuf;

	ubStrLen = 0;

	ubUserDataBuf[sciid][ubStrLen++] = 'W';
	ubUserDataBuf[sciid][ubStrLen++] = 'A';
	ubUserDataBuf[sciid][ubStrLen++] = 'T';
	ubUserDataBuf[sciid][ubStrLen++] = 'E';
	ubUserDataBuf[sciid][ubStrLen++] = 'R';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'S';		// 模式
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = 's';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	ubUserDataBuf[sciid][ubStrLen++] = uwSetStatus + '0';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';	

	ubUserDataBuf[sciid][ubStrLen++] = 'W';		// Water状态
	ubUserDataBuf[sciid][ubStrLen++] = 'a';
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = 'r';
	ubUserDataBuf[sciid][ubStrLen++] = ':';		// 
	ubUserDataBuf[sciid][ubStrLen++] = SysSts1.Bits.Water + '0';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'X';		// 消毒
	ubUserDataBuf[sciid][ubStrLen++] = 'D';
	ubUserDataBuf[sciid][ubStrLen++] = 's';
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.DisinfTime, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'T';		// CUP时间
	ubUserDataBuf[sciid][ubStrLen++] = 'i';
	ubUserDataBuf[sciid][ubStrLen++] = 'm';
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)udwDisinfCnt, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'W';		// 冲水时间
	ubUserDataBuf[sciid][ubStrLen++] = 'T';
	ubUserDataBuf[sciid][ubStrLen++] = 's';
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.WashCase, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'T';		// CUP时间
	ubUserDataBuf[sciid][ubStrLen++] = 'i';
	ubUserDataBuf[sciid][ubStrLen++] = 'm';
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)udwWaterCnt, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	sSciWrite(sciid,ubUserDataBuf[sciid],ubStrLen);

}



void sTP1Command(INT8U sciid) // TP1设置
{
	INT8U  ubStrLen;
	INT8U  *pDataBuf;

	ubStrLen = 0;

	ubUserDataBuf[sciid][ubStrLen++] = 'P';
	ubUserDataBuf[sciid][ubStrLen++] = '1';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'S';		// 模式
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = 's';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	if(uwMotorCtrMode == cMotorP1Mode)
	{
		ubUserDataBuf[sciid][ubStrLen++] = 'P';
		ubUserDataBuf[sciid][ubStrLen++] = '1';
	}
	else
	{
		ubUserDataBuf[sciid][ubStrLen++] = 'N';
		ubUserDataBuf[sciid][ubStrLen++] = 'A';
	}
	ubUserDataBuf[sciid][ubStrLen++] = ' ';	

	ubUserDataBuf[sciid][ubStrLen++] = 'B';		// 电机是否开始运作
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = 'g';
	ubUserDataBuf[sciid][ubStrLen++] = 'i';
	ubUserDataBuf[sciid][ubStrLen++] = 'n';
	ubUserDataBuf[sciid][ubStrLen++] = ':';		// 
	if(SysBit_1.Bits.PCtrBegin)
	{
		ubUserDataBuf[sciid][ubStrLen++] = '1';	// 
	}
	else
	{
		ubUserDataBuf[sciid][ubStrLen++] = '0';	// 
	}
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'S';		// 
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = 'a';
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = 'C';
	ubUserDataBuf[sciid][ubStrLen++] = 'a';
	ubUserDataBuf[sciid][ubStrLen++] = 's';
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = ':';		//
	ubUserDataBuf[sciid][ubStrLen++] = uwP_SeatCtrCase + '0';	// 
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'C';		// 
	ubUserDataBuf[sciid][ubStrLen++] = 'u';
	ubUserDataBuf[sciid][ubStrLen++] = 'r';
	ubUserDataBuf[sciid][ubStrLen++] = 'r';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)CurrPst.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'M';		// 
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = 'm';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.P1.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'S';		// 
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = 'B';
	ubUserDataBuf[sciid][ubStrLen++] = 'k';
	ubUserDataBuf[sciid][ubStrLen++] = 'C';
	ubUserDataBuf[sciid][ubStrLen++] = 'a';
	ubUserDataBuf[sciid][ubStrLen++] = 's';
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = ':';		//
	ubUserDataBuf[sciid][ubStrLen++] = uwP_StBkCtrCase + '0';	// 
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	ubUserDataBuf[sciid][ubStrLen++] = 'C';		// 
	ubUserDataBuf[sciid][ubStrLen++] = 'u';
	ubUserDataBuf[sciid][ubStrLen++] = 'r';
	ubUserDataBuf[sciid][ubStrLen++] = 'r';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)CurrPst.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'M';		// 
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = 'm';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.P1.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	sSciWrite(sciid,ubUserDataBuf[sciid],ubStrLen);

}

void sTLPCommand(INT8U sciid) // TLP设置
{
	INT8U  ubStrLen;
	INT8U  *pDataBuf;

	ubStrLen = 0;

	ubUserDataBuf[sciid][ubStrLen++] = 'L';
	ubUserDataBuf[sciid][ubStrLen++] = 'P';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = '-';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	ubUserDataBuf[sciid][ubStrLen++] = 'M';		// 电机模式
	ubUserDataBuf[sciid][ubStrLen++] = 'o';
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = 'o';
	ubUserDataBuf[sciid][ubStrLen++] = 'r';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	ubUserDataBuf[sciid][ubStrLen++] = uwMotorCtrMode + '0';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';	

	ubUserDataBuf[sciid][ubStrLen++] = 'L';		// 吐痰状态
	ubUserDataBuf[sciid][ubStrLen++] = 'P';
	ubUserDataBuf[sciid][ubStrLen++] = ':';		// 
	ubUserDataBuf[sciid][ubStrLen++] = uwSpiteSts + '0';
	ubUserDataBuf[sciid][ubStrLen++] = ' ';	

	// 保存Pos	
	ubUserDataBuf[sciid][ubStrLen++] = 'S';		// 
	ubUserDataBuf[sciid][ubStrLen++] = 'a';
	ubUserDataBuf[sciid][ubStrLen++] = 'v';
	ubUserDataBuf[sciid][ubStrLen++] = 'e';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)RecordPst.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)RecordPst.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	
	// 极限Pos	
	ubUserDataBuf[sciid][ubStrLen++] = 'L'; 	// 
	ubUserDataBuf[sciid][ubStrLen++] = 'm';
	ubUserDataBuf[sciid][ubStrLen++] = 't';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.UpLmt.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)MemPlace.UpLmt.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	// 当前Pos	
	ubUserDataBuf[sciid][ubStrLen++] = 'C'; 	// 
	ubUserDataBuf[sciid][ubStrLen++] = 'u';
	ubUserDataBuf[sciid][ubStrLen++] = 'r';
	ubUserDataBuf[sciid][ubStrLen++] = ':';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)CurrPst.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX((INT16U)CurrPst.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	sSciWrite(sciid,ubUserDataBuf[sciid],ubStrLen);

}


void sTCGQ1Command(INT8U sciid) // 传感器/角度数据
{
	INT8U  ubStrLen;
	INT8U  *pDataBuf;

	ubStrLen = 0;

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	(void)subNumToXX(ubFaultCode,0,pDataBuf);
	ubStrLen += 2;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	ubStrLen += SafeInt16ToDecAscii(AccelStBk.x,(char*)pDataBuf);
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	ubStrLen += SafeInt16ToDecAscii(AccelStBk.y,(char*)pDataBuf);
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	ubStrLen += SafeInt16ToDecAscii(AccelStBk.z,(char*)pDataBuf);
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX(CurrPst.P_StBk, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX(uwTest, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX(uwTest1, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX(uwTest2, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';
	
	sSciWrite(sciid,ubUserDataBuf[sciid],ubStrLen);

}

void sTCGQ2Command(INT8U sciid) // 传感器/角度数据
{
	INT8U  ubStrLen;
	INT8U  *pDataBuf;

	ubStrLen = 0;

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	(void)subNumToXX(ubFaultCode,0,pDataBuf);
	ubStrLen += 2;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	ubStrLen += SafeInt16ToDecAscii(AccelSeat.x,(char*)pDataBuf);
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	ubStrLen += SafeInt16ToDecAscii(AccelSeat.y,(char*)pDataBuf);
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	ubStrLen += SafeInt16ToDecAscii(AccelSeat.z,(char*)pDataBuf);
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	pDataBuf = &ubUserDataBuf[sciid][ubStrLen];
	subNumToXXXXX(CurrPst.P_Seat, 0, pDataBuf);
	ubStrLen += 5;
	ubUserDataBuf[sciid][ubStrLen++] = ' ';

	sSciWrite(sciid,ubUserDataBuf[sciid],ubStrLen);

}


/******************************************************************************
Function Name:
    subNumToXX: Convert input uwNumber into 2 digital numbers represented by
          ASCII code.
Parameters:
    bExponent: the 10 exponent of input Number
    uwNumber: Number to be converted into ASCII code
    pubBuffer: pointer of conversion register
Internal Variable:
    uwNumFormated: Number after formatting
    ubConvResult: conversion result
Return Value:
    true:   1 ( The Number to be converted is normal )
    false:  0 ( The Number to be converted is over 99, and limited to 99 )
*******************************************************************************/
uint8_t subNumToXX(const uint16_t uwNumber, const int8_t bExponent, uint8_t *pubBuffer)
{
    uint16_t  uwNumFormated = 0;
    uint8_t   ubConvResult = 0;
    
    uwNumFormated = (uint16_t)sudwNumFormat((uint32_t)uwNumber, bExponent, 0);
    ubConvResult = true;
    
    if(uwNumFormated > 99)
    {
        uwNumFormated = 99;
        ubConvResult = false;
    }
    
    *(pubBuffer) = subNumAsciiPick((uint32_t)uwNumFormated,2);
    *(pubBuffer + 1) = subNumAsciiPick((uint32_t)uwNumFormated,1);
    *(pubBuffer + 2) = '\0';
    return(ubConvResult);
}



/******************************************************************************
Function Name:
    subNumToXXXXX: Convert input uwNumber into 5 digital numbers 
Parameters:
    bExponent: the 10 exponent of input Number
    udwNumber: Number to be converted into ASCII code
    pubBuffer: pointer of conversion register
Internal Variable:
    uwNumFormated: Number after formatting
    ubConvResult: conversion result
Return Value:
    true:   1 ( The Number to be converted is normal )
    false:  0 ( The Number to be converted is over 99999, and limited to 99999 )
*******************************************************************************/
uint8_t subNumToXXXXX(uint32_t udwNumber, int8_t bExponent, uint8_t *pubBuffer)
{
    uint32_t  udwNumFormated = 0;
    uint8_t   ubConvResult = 0;
//	uint8_t   ubTemp;

//	ubTemp = bExponent;
	udwNumFormated = sudwNumFormat(udwNumber, bExponent, 0);
    ubConvResult = true;
        
    if(udwNumFormated > 99999)
    {
        udwNumFormated = 99999;
        ubConvResult = false;
    }
    
    *(pubBuffer) = subNumAsciiPick(udwNumFormated,5);
    *(pubBuffer + 1) = subNumAsciiPick(udwNumFormated,4);
    *(pubBuffer + 2) = subNumAsciiPick(udwNumFormated,3);
    *(pubBuffer + 3) = subNumAsciiPick(udwNumFormated,2);
    *(pubBuffer + 4) = subNumAsciiPick(udwNumFormated,1);
    *(pubBuffer + 5) = '\0';
    return(ubConvResult);
}


/******************************************************************************
Function Name:
    subNumAsciiPick: Convert the secleced digital to Ascii code
Parameters:
    udwNumber: Number to be converted into ASCII code
    ubPosition:
Return Value:
    ubCode
*******************************************************************************/
uint8_t   subNumAsciiPick(uint32_t udwNumber, uint8_t ubPosition)
{
    uint8_t   ubCode = 0;
    switch(ubPosition)
    {
        case 1: 
            ubCode = udwNumber % 10 + '0';
            break;
        case 2:
            ubCode = (udwNumber / 10) % 10 + '0';
            break;
        case 3:
            ubCode = (udwNumber / 100) % 10 + '0';
            break;
        case 4:
            ubCode = (udwNumber / 1000) % 10 + '0';
            break;
        case 5:
            ubCode = udwNumber / 10000 + '0';
            break;
        default:
            ubCode = udwNumber % 10 + '0';
            break;
    }
    return(ubCode);
}

/******************************************************************************
Function Name:
    sudwNumFormat: Convert input uwNumber and its Exponent into Number with
            0, 1, or 2 decimal digitals.
Parameters:
    bExponent: the 10 exponent of input Number
    udwNumber: Number to be converted into ASCII code
Return Value:
    udwNumber
*******************************************************************************/
uint32_t  sudwNumFormat(uint32_t udwNumber, int8_t bExponent, int8_t bDecimal)
{
    while(bExponent != bDecimal)
    {
        if(bExponent > bDecimal)
        {
            if(udwNumber <= 429496729)
            {
                udwNumber = udwNumber * 10;
            }
            else
            {
                udwNumber = 4294967295;
            }
            bExponent--;
        }
        else
        {
            if(udwNumber != 0)
            {
                udwNumber = udwNumber / 10;
            }
            bExponent++;
        }
    }
    
    return(udwNumber);
}





/**
  * @brief  将有符号16位整数转换为十进制ASCII字符串（优化版）
  * @param  value: 要转换的16位有符号数
  * @param  buffer: 存放结果的缓冲区（至少7字节）
  * @retval 转换后的字符串长度
  */
uint8_t Int16ToDecAscii_Optimized(int16_t value, char* buffer)
{
    uint16_t divisor = 10000;
    uint8_t started = 0;
    uint8_t length = 0;
//    uint8_t isNegative = 0;
    
    // 处理负数
    if (value < 0)
    {
        buffer[length++] = '-';
        value = -value;
//        isNegative = 1;
    }
    
    // 处理0的特殊情况
    if (value == 0)
    {
        buffer[length++] = '0';
        buffer[length] = '\0';
        return length;
    }
    
    // 最大16位有符号数是32767，所以从10000开始
    while (divisor > 0)
    {
        uint8_t digit = value / divisor;
        
        // 如果是第一个非零数字或最后一位数字
        if (digit != 0 || started || divisor == 1)
        {
            buffer[length++] = digit + '0';
            started = 1;
        }
        
        value %= divisor;
        divisor /= 10;
    }
    
    buffer[length] = '\0'; // 添加字符串结束符
    
    return length;
}





/**
  * @brief  安全的有符号16位整数转十进制ASCII
  * @param  value: 要转换的16位有符号数
  * @param  buffer: 存放结果的缓冲区（至少7字节）
  * @retval 转换后的字符串长度
  */
uint8_t SafeInt16ToDecAscii(int16_t value, char* buffer)
{
    // 处理最小负数(-32768)的特殊情况
    if (value == -32768)
    {
        const char* min = "-32768";
        uint8_t i = 0;
        while (min[i] != '\0')
        {
            buffer[i] = min[i];
            i++;
        }
        buffer[i] = '\0';
        return i;
    }
    
    // 其他情况使用优化版本
    return Int16ToDecAscii_Optimized(value, buffer);
}

/******************************************************************************/
//Function name : sGetAllSendSts
//Description   : 获取主/副 串口发送状态
//                如果都空闲，返回1，否则返回0
//                
//                
//Parameters    : None
//Return        : None
/******************************************************************************/
INT8U sGetAllSendStsIdle(void)
{
	if(sSciGetTxStatus(ComDevice1) == cSciTxRdy && sSciGetTxStatus(ComDevice1) == cSciTxRdy)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}





